package cc.linker.steplink.crm.service.impl;

import cc.linker.steplink.crm.dao.LabelDao;
import cc.linker.steplink.crm.dao.LabelRelationDao;
import cc.linker.steplink.crm.domain.Label;
import cc.linker.steplink.crm.response.GlobalException;
import cc.linker.steplink.crm.response.ResponseException;
import cc.linker.steplink.crm.service.ILabelService;
import com.alibaba.fastjson.JSON;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class LabelServiceImpl implements ILabelService {

    @Autowired
    private LabelDao labelDao;

    @Autowired
    private LabelRelationDao labelRelationDao;
    

    @Override
    public List<Map<String, Object>> listLabel(Map<String, Object> map) throws Exception
    {
        return  labelDao.listLabel(map);
    }

    @Override
    public Map<String, Object> getLabel(String labelId) throws Exception {
        if (StringUtils.isEmpty(labelId)) {
            throw new GlobalException(new ResponseException(400, "标签ID不能为空"));
        }
        Map<String, Object> label = labelDao.getLabel(labelId);
        Object autoSetup = label.get("autoSetup");
        Object auto = JSON.parse((String) autoSetup);
        label.put("autoSetup",auto);
        return label;
    }

    @Override
    public String getLabelIdByName(Map<String, String> map) throws Exception
    {
        return labelDao.getLabelIdByName(map);

    }


    @Override
    public Boolean addLabel(Label record) throws Exception {
        if(StringUtils.isEmpty(record.getLabelName())){
            throw new GlobalException(new ResponseException(400, "标签名不能为空"));
        }
        if(labelDao.ifLabelExist(record)>0){
            throw new GlobalException(new ResponseException(400, "该标签已存在"));
        }
        int id = labelDao.insert(record);
        if(id>0){
            return true;
        }
        throw new GlobalException(new ResponseException(400, "添加标签失败"));
    }

    @Override
    public Boolean updateLabel(Label record) throws Exception {
        if(StringUtils.isEmpty(record.getLabelName())){
            throw new GlobalException(new ResponseException(400, "标签名不能为空"));
        }
        if(StringUtils.isEmpty(record.getLabelId())){
            throw new GlobalException(new ResponseException(400, "标签id不能为空"));
        }
        if(labelDao.ifLabelExist(record)>0){
            throw new GlobalException(new ResponseException(400, "该标签已存在"));
        }
        int count = labelDao.updateByPrimaryKeySelective(record);
        if(count>0){
            return true;
        }
        throw new GlobalException(new ResponseException(400, "更新标签失败"));
    }

    @Override
    public Boolean delLabel(String labelId) throws GlobalException
    {
        //删除关联记录
        labelRelationDao.deleteByLabelId(labelId);
        int delCount = labelDao.deleteByPrimaryKey(labelId);
        labelDao.deleteGoodsRel(labelId);
        if(delCount>0){
            return true;
        }
        throw new GlobalException(new ResponseException(400, "删除标签失败"));
    }

    @Override
    public List<Map<String, Object>> getLabelsByOrgId(String orgId) throws Exception
    {
        return labelDao.getLabelsByOrgId(orgId);
    }

    @Override
    public List<Map<String, Object>> getArticleLabelsByOrgId(String orgId) throws Exception
    {
        return  labelDao.getArticleLabelsByOrgId(orgId);
    }


    @Override
    public Boolean addCustomLabel(List<Map<String,Object>> list,String customerId) throws Exception
    {
        if(StringUtils.isEmpty(customerId)){
            throw new GlobalException(new ResponseException(400, "客户ID不能为空"));
        }
        String oldLabels = labelRelationDao.getLabelsByCustomerId(customerId);
        List<Map<String,Object>>  newList = new ArrayList();
        StringBuilder labels = new StringBuilder();
        for (Map<String,Object> label : list){
            labels.append(label.get("labelId").toString()).append(",");
            //
            if(oldLabels==null || !oldLabels.contains(label.get("labelId").toString())){
                newList.add(label);
            }
        }
        String labelString = labels.length()>0?labels.substring(0,labels.length()-1):"";
        labelRelationDao.deleteByCustomerId(customerId, labelString);
        if(newList.isEmpty()){
            return true;
        }
        int insertCount = labelRelationDao.insertAll(newList);
        if(insertCount>0){
            return true;
        }
        throw new GlobalException(new ResponseException(400, "添加用户标签失败"));
    }


    @Override
    public List<Map<String, Object>> getLabelList(String orgId,Integer labelType) throws Exception
    {
        //现在没有类型，兼容之前的
        labelType = null;
        return labelDao.getLabelList(orgId,labelType);
    }
}
